﻿
using DocumentGeneratorPlugins;
using Microsoft.XmlDiffPatch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DocumentorUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 


    public partial class MainWindow : Window
    {

        ISystemDocumentor _CurrentObj = null;
        public MainWindow()
        {
            InitializeComponent();
            Assembly[] lstAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            var type = typeof(ISystemDocumentor);
            var types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => type.IsAssignableFrom(p) && p.IsInterface==false);
            foreach(Type t in types)
            {
                ISystemDocumentor o = (ISystemDocumentor)Activator.CreateInstance(t);
                string strpName= o.PluginName;
                MenuItem mi=new MenuItem();
                mi.Header=strpName;
                mi.Click += mi_Click;
                mi.Tag = o;
                o.DocumentGeneratorEvent += o_DocumentGeneratorEvent;
                mnuPlugins.Items.Add(mi);
            }
            
        }

        void o_DocumentGeneratorEvent(object sender, DocumentGeneratorEventArgs e)
        {
            this.Dispatcher.BeginInvoke((Action)(() => { sbStatus.Text = e.Message; }));
        }

        void mi_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem ;
            _CurrentObj = mi.Tag as ISystemDocumentor;
            PluginUIContainer.Children.Clear();
            PluginUIContainer.Children.Add( _CurrentObj.GetUserInterface());
            btnStart.IsEnabled = true;

        }

       

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _CurrentObj.DocumentGeneratorEvent += Doc_DocumentGeneratorEvent;
            btnStart.IsEnabled = false;
            System.Threading.ThreadStart ts = new System.Threading.ThreadStart(StartDocumentGeneration);
            System.Threading.Thread th = new System.Threading.Thread(ts);
            th.Start();
        }
        private void StartDocumentGeneration()
        {
            if (_CurrentObj == null)
                return;
        }
        void Doc_DocumentGeneratorEvent(object sender, DocumentGeneratorPlugins.DocumentGeneratorEventArgs e)
        {
           

        }

        private void mnuCompare_Click(object sender, RoutedEventArgs e)
        {
            PluginUIContainer.Children.Clear();
            PluginUIContainer.Children.Add(new XMLCompareUC());

        }
        

        
        
    }
}
